const { resolve } = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const url = require('url');

const publicPath = '/page/';

module.exports = (options = {}) => {
  let processEnv = {
    NODE_ENV: '"production"',
  };
  if (options.dev === 'development') {
    processEnv = {
      NODE_ENV: '"development"',
    };
  } else if (options.dev === 'build_test') {
    processEnv = {
      NODE_ENV: '"test"',
    };
  }
  const ret = ({
    entry: {
      index: ['babel-polyfill', './src/front-end/app/js/main.js'],
      h5: './src/front-end/h5/js/main.js',
    },
    output: {
      path: resolve(__dirname, 'dist/page'),
      filename: options.dev === 'development' ? '[name].js' : '[name].js?[chunkhash]',
      chunkFilename: '[id].js?[chunkhash]',
      publicPath,
    },
    module: {
      rules: [{
        test: /\.vue$/,
        use: ['vue-loader'],
      },
      {
        test: /\.js$/,
        use: ['babel-loader'],
        exclude: /node_modules/,
      },
      // {
      //   enforce: 'pre',
      //   test: /\.(js|vue)$/,
      //   use: [{
      //     loader: 'eslint-loader',
      //   }],
      //   exclude: /node_modules/,
      // },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader', 'postcss-loader'],
      },
      {
        test: /\.(png|jpg|jpeg|gif|eot|ttf|woff|woff2|svg|svgz)(\?.+)?$/,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 10000,
          },
        }],
      },
      ],
    },
    plugins: [

      new webpack.DefinePlugin({
        'process.env': processEnv,
      }),
      new webpack.optimize.CommonsChunkPlugin({
        names: ['manifest'],
      }),
      new HtmlWebpackPlugin({
        chunks: ['index', 'manifest'],
        template: 'src/page/index.html',
        filename: resolve(__dirname, 'dist/page/index.html'),
        favicon: 'src/page/qing.ico'
        // filename: './page/index.html',
      }),
      new HtmlWebpackPlugin({
        chunks: ['h5', 'manifest'],
        template: 'src/page/h5.html',
        filename: resolve(__dirname, 'dist/page/h5.html'),
        // filename: './page/login.html'
      }),
    ],
    resolve: {
      alias: {
        '~': resolve(__dirname, 'src'),
        '@': resolve(__dirname, 'src/front-end/app/js/component'),
      },
      extensions: ['.js', '.vue', '.json', '.css'],
    },
    devServer: {
      host: '0.0.0.0',
      port: 8054,
      // port: 8050,
      disableHostCheck: true,
      proxy: {
        '/api/': {
          target: 'https://test-component.qingclass.com',
          // target: 'http://0.0.0.0:8080',
          changeOrigin: true,
          pathRewrite: {
            '^/api': '',
          },
        },
      },
      // publicPath: '/page/',
      historyApiFallback: {
        index: url.parse(options.dev === 'development' ? '/assets/' : publicPath).pathname,
      },
    },
    devtool: options.dev === 'development' ? '#eval-source-map' : '#source-map',
  });
  return ret;
};
